
#include "config.h"
#include "led.h"
#include "iic1.h"
#include "iic3.h"
#include "adc.h"
#include "spi.h"
#include "uart.h"
#include "uartHandle.h"
#include "icm42688.h"
#include "vi5300.h"
#include "timer.h"
#include <math.h>
#include "bl602StateHandle.h"
#include "protocol.h"
#include "app_task.h"
#include "gpio.h"
#include "appControl.h"
#include "diode.h"
#include <stdio.h>

/**
 * @brief  BSP clock initialize.
 *         Set board system clock to MPLL@200MHz
 * @param  None
 * @retval None
 */
void BSP_CLK_Init(void)
{
    stc_clk_sysclk_cfg_t stcSysClkCfg;
    stc_clk_xtal_cfg_t stcXtalCfg;
    stc_clk_mpll_cfg_t stcMpllCfg;
    stc_sram_config_t stcSramConfig;

    MEM_ZERO_STRUCT(stcSysClkCfg);
    MEM_ZERO_STRUCT(stcXtalCfg);
    MEM_ZERO_STRUCT(stcMpllCfg);
    MEM_ZERO_STRUCT(stcSramConfig);

    /* Set bus clk div. */
    //    stcSysClkCfg.enHclkDiv = ClkSysclkDiv1;
    //    stcSysClkCfg.enExclkDiv = ClkSysclkDiv2;
    //    stcSysClkCfg.enPclk0Div = ClkSysclkDiv1;
    //    stcSysClkCfg.enPclk1Div = ClkSysclkDiv2;
    //    stcSysClkCfg.enPclk2Div = ClkSysclkDiv4;
    //    stcSysClkCfg.enPclk3Div = ClkSysclkDiv4;
    //    stcSysClkCfg.enPclk4Div = ClkSysclkDiv2;
    //    CLK_SysClkConfig(&stcSysClkCfg);

    /* Config Xtal and Enable Xtal */
    stcXtalCfg.enMode = ClkXtalModeOsc;
    stcXtalCfg.enDrv = ClkXtalLowDrv;
    stcXtalCfg.enFastStartup = Enable;
    CLK_XtalConfig(&stcXtalCfg);
    CLK_XtalCmd(Enable);

    /* sram init include read/write wait cycle setting */
    stcSramConfig.u8SramIdx = Sram12Idx | Sram3Idx | SramHsIdx | SramRetIdx;
    stcSramConfig.enSramRC = SramCycle2;
    stcSramConfig.enSramWC = SramCycle2;
    SRAM_Init(&stcSramConfig);

    /* flash read wait cycle setting */
    EFM_Unlock();
    EFM_SetLatency(EFM_LATENCY_5);
    EFM_Lock();

    /* MPLL config (XTAL / pllmDiv * plln / PllpDiv = 200M). */
    stcMpllCfg.pllmDiv = 1ul;
    stcMpllCfg.plln = 50ul;
    stcMpllCfg.PllpDiv = 2ul;
    stcMpllCfg.PllqDiv = 2ul; // 16ul;//2
    stcMpllCfg.PllrDiv = 2ul; // 16ul;//2
    CLK_SetPllSource(ClkPllSrcXTAL);
    CLK_MpllConfig(&stcMpllCfg);

    /* Enable MPLL. */
    CLK_MpllCmd(Enable);
    /* Wait MPLL ready. */
    while (Set != CLK_GetFlagStatus(ClkFlagMPLLRdy))
    {
        ;
    }
    /* Switch driver ability */
    PWC_HS2HP();
    /* Switch system clock source to MPLL. */
    CLK_SetSysClkSource(CLKSysSrcMPLL);
}

static void init(void)
{			
    BSP_CLK_Init();		
    i2c1Init();
    i2c3Init();
    uartInit(); 
    timerInit(); 
    adcInit();
	gpioShakeConfig();
	gpioLedConfig();
	diodeInit();
	vi5300IsExistFlag = vi5300Init();
	
	ledcontralred();
	Ddl_Delay1ms(500);
	ledcontralgreen();
	Ddl_Delay1ms(500);
	ledcontralblue();
	Ddl_Delay1ms(500);
	ledcontralblack();
}
void test(void)
{
	//printf("take time\r\n");
}
int32_t main(void)
{
    init();
	
    uint32_t t = 0;
    float dt;
    float sumDt = 0; 
    float sumDt2 = 0; 
    uint8_t vi5300IsOkFlag = vi5300Init();
	
	while (1)
	{ 	
		test();
		timerUpdate();
		dt = getDt(&t);
		sumDt += dt;
		sumDt2 += dt;
		pollfunction();
		if (sumDt > 0.001f)
		{
			sumDt = 0;
		}		
		if (sumDt2 > 0.100f)
		{
			Usart3RxErrProcess();
			Usart1RxErrProcess();
			shakeControl(sumDt2);
			taskVi5300taskReadADC();
			sumDt2 = 0;
		}
	}
}	

	